<?php

namespace App\Admin\Controllers;

use App\Admin\Models\Category;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use \Log;
use \Cache;
use Encore\Admin\Tree;

class CategoryController extends Controller
{
    use ModelForm;
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('分类');
            $content->description('分类列表');
            $content->body($this->tree());
//            $content->body($this->grid());
        });
    }

    protected function tree()
    {
        return Category::tree(function (Tree $tree) {

            $tree->branch(function ($branch) {
                $src = env('BASE_URL') . '/' . $branch['img'] ;
                $logo = '';
                if(!empty($branch['img'])){
                    $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>";
                }
                return "{$branch['id']} - {$branch['title']} $logo";
            });
        });
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('管理');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('管理');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('管理');
            $content->description('添加');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Admin::grid(Category::class, function (Grid $grid) {
            $grid->id('用户id')->sortable();
            $grid->name('名称');
            $grid->parent_id('父级id');
            $grid->sort('排序');
            $grid->img('广告图片')->image(100,100);
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Category::class, function (Form $form) {

            $form->select('parent_id', '父级id')->options(Category::selectOptions(null,'顶级'));
            $form->text('title', '名称')->required(); //->rules('max:10|min:3');
            $form->radio('status', '是否展示')->options([1 => '展示',2 => '不展示']);
            $form->radio('type', '分类')->options([1 => '文章',2 => '图片',3 => '小说',4 => '视频']);
            $form->image('img','图片')->uniqueName();
        });
    }
}
